package com.ruoyi.archives.service.impl;

import java.util.List;

import com.ruoyi.archives.domain.CateringRecord;
import com.ruoyi.archives.domain.CateringUnit;
import com.ruoyi.archives.mapper.CateringRecordMapper;
import com.ruoyi.archives.mapper.CateringUnitMapper;
import com.ruoyi.archives.service.ICateringRecordService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 供餐单位供餐记录Service业务层处理
 *
 * @author wng
 * @date 2024-08-04
 */
@Service
public class CateringRecordServiceImpl implements ICateringRecordService {
    @Resource
    private CateringRecordMapper cateringRecordMapper;
    @Resource
    private CateringUnitMapper cateringUnitMapper;

    /**
     * 查询供餐单位供餐记录
     *
     * @param id 供餐单位供餐记录主键
     * @return 供餐单位供餐记录
     */
    @Override
    public CateringRecord selectCateringRecordById(Long id) {
        CateringRecord cateringRecord = cateringRecordMapper.selectCateringRecordById(id);
        CateringUnit cateringUnit = cateringUnitMapper.selectCateringUnitById(cateringRecord.getUnitId());
        if (cateringUnit != null) {
            cateringRecord.setUnitName(cateringUnit.getName());
//            cateringRecord.getParams().put("unitName", cateringUnit.getName());
        }
        return cateringRecord;
    }

    /**
     * 查询供餐单位供餐记录列表
     *
     * @param cateringRecord 供餐单位供餐记录
     * @return 供餐单位供餐记录
     */
    @Override
    public List<CateringRecord> selectCateringRecordList(CateringRecord cateringRecord) {
        if (cateringRecord.getParams() != null) {
            if (cateringRecord.getParams().containsKey("createTimeStart") && cateringRecord.getParams().containsKey("createTimeEnd")) {
                if (cateringRecord.getParams().get("createTimeStart") != null && !"".equals(cateringRecord.getParams().get("createTimeStart"))
                        && cateringRecord.getParams().get("createTimeEnd") != null && !"".equals(cateringRecord.getParams().get("createTimeEnd"))) {
                    cateringRecord.getParams().put("createTimeStart", cateringRecord.getParams().get("createTimeStart") + " 00:00:00");
                    cateringRecord.getParams().put("createTimeEnd", cateringRecord.getParams().get("createTimeEnd") + " 23:59:59");
                }
            }

            if (cateringRecord.getParams().containsKey("deliveryTimeStart") && cateringRecord.getParams().containsKey("deliveryTimeEnd")) {
                if (cateringRecord.getParams().get("deliveryTimeStart") != null && !"".equals(cateringRecord.getParams().get("deliveryTimeStart"))
                        && cateringRecord.getParams().get("deliveryTimeEnd") != null && !"".equals(cateringRecord.getParams().get("deliveryTimeEnd"))) {
                    cateringRecord.getParams().put("deliveryTimeStart", cateringRecord.getParams().get("deliveryTimeStart") + " 00:00:00");
                    cateringRecord.getParams().put("deliveryTimeEnd", cateringRecord.getParams().get("deliveryTimeEnd") + " 23:59:59");
                }

            }
        }
        List<CateringRecord> cateringRecords = cateringRecordMapper.selectCateringRecordList(cateringRecord);
        cateringRecords.forEach(item -> {
            CateringUnit cateringUnit = cateringUnitMapper.selectCateringUnitById(item.getUnitId());
            if (cateringUnit != null) {
                item.setUnitName(cateringUnit.getName());
//                item.getParams().put("unitName", cateringUnit.getName());
            }
        });
        return cateringRecords;
    }

    /**
     * 新增供餐单位供餐记录
     *
     * @param cateringRecord 供餐单位供餐记录
     * @return 结果
     */
    @Override
    public int insertCateringRecord(CateringRecord cateringRecord) {
        cateringRecord.setCreateTime(DateUtils.getNowDate());
        return cateringRecordMapper.insertCateringRecord(cateringRecord);
    }

    /**
     * 修改供餐单位供餐记录
     *
     * @param cateringRecord 供餐单位供餐记录
     * @return 结果
     */
    @Override
    public int updateCateringRecord(CateringRecord cateringRecord) {
        cateringRecord.setUpdateTime(DateUtils.getNowDate());
        return cateringRecordMapper.updateCateringRecord(cateringRecord);
    }

    /**
     * 批量删除供餐单位供餐记录
     *
     * @param ids 需要删除的供餐单位供餐记录主键
     * @return 结果
     */
    @Override
    public int deleteCateringRecordByIds(Long[] ids) {
        return cateringRecordMapper.deleteCateringRecordByIds(ids);
    }

    /**
     * 删除供餐单位供餐记录信息
     *
     * @param id 供餐单位供餐记录主键
     * @return 结果
     */
    @Override
    public int deleteCateringRecordById(Long id) {
        return cateringRecordMapper.deleteCateringRecordById(id);
    }
}
